package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.impl.SysRoleServiceImpl;
import com.atguigu.spzx.manager.entity.SysRole;
import com.atguigu.spzx.common.result.Result;
import com.atguigu.spzx.manager.annotation.Log;
import com.atguigu.spzx.manager.enums.BusinessType;
import com.atguigu.spzx.manager.vo.sysRole.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * SysRoleController
 * </p>
 *
 * @author flyduck
 * @since 2023/10/23
 */
@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleServiceImpl sysRoleServiceImpl;

    @ApiOperation("给角色分配菜单权限")
    @PostMapping("/assignMenuForRole")
    public Result assignMenuForRole(@RequestBody SysRoleAssignMenuReqVo reqVo){
        sysRoleServiceImpl.assignMenuForRole(reqVo);
        return Result.ok();
    }

    @ApiOperation("根据角色获取菜单权限")
    @GetMapping("/getAssignedMenuListByRoleId/{roleId}")
    public Result<List<SysRoleAssignMenuQueryVo>> getAssignedMenuListByRoleId(@PathVariable("roleId") Long roleId){
        List<SysRoleAssignMenuQueryVo> list = sysRoleServiceImpl.getAssignedMenuListByRoleId(roleId);
        return Result.ok(list);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除")
    @DeleteMapping("/deleteRoleListByIdList")
    public Result deleteRoleListByIdList(@RequestBody List<Long> idList){
        sysRoleServiceImpl.deleteRoleListByIdList(idList);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation("修改角色")
    @PostMapping("/updateRoleById")
    public Result<Void> updateRoleById(@RequestBody SysRoleUpdateReqVo sysRoleSaveReqVo){
        sysRoleServiceImpl.updateRoleById(sysRoleSaveReqVo);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("根据id查询")
    @PostMapping("/getRoleById/{id}")
    public Result<SysRoleQueryRespVo> getRoleById(@PathVariable("id") Long id){
        SysRoleQueryRespVo sysRoleQueryRespVo = sysRoleServiceImpl.getRoleById(id);
        return Result.ok(sysRoleQueryRespVo);
    }

    @Log(title = "角色管理",businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation("添加角色")
    @PostMapping("/saveRole")
    public Result<Void> saveRole(@RequestBody SysRoleSaveReqVo sysRoleSaveReqVo){
        sysRoleServiceImpl.saveRole(sysRoleSaveReqVo);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("/getRolePageByQuery/{page}/{limit}")
    public Result<Page<SysRoleQueryRespVo>> getRolePageByQuery(@PathVariable("page") Long page,
                                                               @PathVariable("limit") Long limit,
                                                               SysRoleQueryReqVo sysRoleQueryReqVo){
        Page<SysRole> pageParam = new Page<>(page, limit);
        Page<SysRoleQueryRespVo> pageModel = sysRoleServiceImpl.getRolePageByQuery(pageParam, sysRoleQueryReqVo);
        return Result.ok(pageModel);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("根据id逻辑删除角色")
    @DeleteMapping("/deleteRoleById/{id}")
    public Result<Void> deleteRoleById(@PathVariable Long id){
        sysRoleServiceImpl.deleteRoleById(id);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("查询所有角色")
    @GetMapping("/getRoleList")
    public Result<List<SysRoleQueryRespVo>> getRoleList(){
        List<SysRoleQueryRespVo> list = sysRoleServiceImpl.getRoleList();
        return Result.ok(list);
    }
}
